package com.devunion.salon.web.form;

import com.devunion.salon.web.bean.CustomerBean;
import com.devunion.salon.web.bean.TransactionBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.LabelValueBean;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author: Timoshenko Alexander
 */
public class TransactionForm extends ActionForm {
    private String hiddenDate;

    private String employee;
    private CustomerBean customerBean;
    private List<LabelValueBean> employees = new ArrayList<LabelValueBean>();
    private List<LabelValueBean> services = new ArrayList<LabelValueBean>();
    private List<LabelValueBean> products = new ArrayList<LabelValueBean>();

    private String selectedService;
    private String selectedProduct;

    private List<TransactionBean> transactions = new ArrayList<TransactionBean>();

    public String amount;
    private Float discount;
    private Float totalPrice;
    private Float totalTaxes;
    private Float totalPriceAndTaxes;


    private String secondPayment;
    private List<String> payments;
    private String secondPaymentAmount;

    private String submittedValue;
    private String delete;

    public TransactionForm() {
        this.customerBean = new CustomerBean();
    }

    public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
        selectedProduct = null;
        selectedService = null;
        employee = null;
        submittedValue = null;
        amount = null;
        delete = null;

        super.reset(actionMapping, httpServletRequest);
    }

    public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
        ActionErrors result = new ActionErrors();
        if (employees.size() > 0 && delete == null) {
            if (StringUtils.isBlank(employee)) {
                result.add(Globals.ERROR_KEY, new ActionMessage("add.transaction.error.employee.empty"));
                return result;
            }

            if (StringUtils.isBlank(selectedProduct) && StringUtils.isBlank(selectedService)) {
                result.add(Globals.ERROR_KEY, new ActionMessage("add.transaction.error.service.product.empty", employees.get(Integer.valueOf(employee)-1).getLabel()));
            }
        }

        return result;
    }

    public String getHiddenDate() throws ParseException {
        return StringUtils.isBlank(hiddenDate) ? String.valueOf(System.currentTimeMillis()) : hiddenDate;
    }

    public void setHiddenDate(String hiddenDate) {
        this.hiddenDate = hiddenDate;
    }

    public String getEmployee() {
        return employee;
    }

    public void setEmployee(String employee) {
        this.employee = employee;
    }

    public List<LabelValueBean> getEmployees() {
        return employees;
    }

    public void setEmployees(List<LabelValueBean> employees) {
        this.employees = employees;
    }

    public List<LabelValueBean> getServices() {
        return services;
    }

    public void setServices(List<LabelValueBean> services) {
        this.services = services;
    }

    public List<LabelValueBean> getProducts() {
        return products;
    }

    public void setProducts(List<LabelValueBean> products) {
        this.products = products;
    }

    public String getSelectedService() {
        return selectedService;
    }

    public void setSelectedService(String selectedService) {
        this.selectedService = selectedService;
    }

    public String getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(String selectedProduct) {
        this.selectedProduct = selectedProduct;
    }

    public List<TransactionBean> getTransactions() {
        return transactions;
    }

    public void setTransactions(List<TransactionBean> transactions) {
        this.transactions = transactions;
    }

    public Float getTotalPrice() {
        this.totalPrice = 0f;
        for (TransactionBean bean : transactions) {
            this.totalPrice += bean.getPrice();
        }
        return totalPrice;
    }

    public void setTotalPrice(Float totalPrice) {
        this.totalPrice = totalPrice;
    }

    public Float getTotalTaxes() {
        totalTaxes = 0f;
        for (TransactionBean bean : transactions) {
            totalTaxes += bean.getTaxes();
        }
        return totalTaxes;
    }

    public void setTotalTaxes(Float totalTaxes) {
        this.totalTaxes = totalTaxes;
    }

    public Float getTotalPriceAndTaxes() {
        totalPriceAndTaxes = 0f;
        for (TransactionBean bean : transactions) {
            totalPriceAndTaxes += bean.getTotal();
        }
        return totalPriceAndTaxes;
    }

    public void setTotalPriceAndTaxes(Float totalPriceAndTaxes) {
        this.totalPriceAndTaxes = totalPriceAndTaxes;
    }

    public Float getDiscount() {
        return discount;
    }

    public void setDiscount(Float discount) {
        this.discount = discount;
    }

    public String getSecondPayment() {
        return secondPayment;
    }

    public void setSecondPayment(String secondPayment) {
        this.secondPayment = secondPayment;
    }

    public List<String> getPayments() {
        return payments;
    }

    public void setPayments(List<String> payments) {
        this.payments = payments;
    }

    public String getSecondPaymentAmount() {
        return secondPaymentAmount;
    }

    public void setSecondPaymentAmount(String secondPaymentAmount) {
        this.secondPaymentAmount = secondPaymentAmount;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getSubmittedValue() {
        return submittedValue;
    }

    public void setSubmittedValue(String submittedValue) {
        this.submittedValue = submittedValue;
    }

    public CustomerBean getCustomerBean() {
        return customerBean;
    }

    public void setCustomerBean(CustomerBean customerBean) {
        this.customerBean = customerBean;
    }

    public String getDelete() {
        return delete;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }
}
